var M_oldSotrIndex;
var M_oldSortItem;
var M_stop = false;
var M_duration = 150;
var M_selecto;
var M_oldSelecto;
var M_oldProductEvent;
var M_isShowForm;
var M_currentProductID;
function sort(index,event){
	if(M_stop) {
		return;
	}
	M_selecto = "#products_"+index;
	if( M_oldSortItem ) {
		if( M_oldSortItem == event.currentTarget ) {
			return;
		}
		M_oldSortItem.className = "";
	}
	M_stop = true;
	event.currentTarget.className = "sel";
	if( M_oldSotrIndex ) {
		M_oldSelecto = "#products_"+M_oldSotrIndex;
		if( M_isShowForm ) {
			$("#form").animate({width: 0},{
				duration: M_duration,
				complete: function() { 
					$("#form").css({'display':'none'});
					hideProduct();
				}
			});
		} else {
			hideProduct();
		}
	} else {
		showeProduct();
	}
	M_oldSotrIndex = index;
	M_oldSortItem = event.currentTarget;
	if( M_oldProductEvent ) {
		M_oldProductEvent.className = "";
		M_oldProductEvent = null;
	}
}
function showeProduct() {
	if( M_oldSelecto ) {
		$(M_oldSelecto).css({'display':'none'});
	}
	$(M_selecto).css({'display':'block',width:0});
	if(!$(M_selecto)[0]) {
		deleteStop();
	}
	$(M_selecto).animate({width: 220},{
		duration: M_duration,
		complete: function() { deleteStop() }
	});
}
function hideProduct() {
	if(!$(M_oldSelecto)[0]) { showeProduct(); }
	$(M_oldSelecto).animate({width: 0},{
		duration: M_duration,
		complete: function() { showeProduct() }
	});
}
function deleteStop(){
	M_stop = false;
}
function product(id,event){
	M_currentProductID = id;
	if( M_oldProductEvent ) {
		if( M_oldProductEvent == event.currentTarget ){
			return;
		}
		M_oldProductEvent.className = "";
		$("#form").animate({width: 0},{
			duration: M_duration,
			complete: function() { showeForm() }
		});
	} else {
		showeForm();
	}
	event.currentTarget.className = "sel";
	M_stop = true;
	M_oldProductEvent = event.currentTarget;
}
function showeForm(){
	M_isShowForm = true;
	$("#form").css({width:0});
	$("#form").css({'display':'block',width:0});
	$.remoteCall('MENU_product','getProductInfo',[M_currentProductID],setProductInfo);
	$("#form").animate({width: 500},{
		duration: M_duration,
		complete: function() { deleteStop() }
	});
}
function callBackRemove(){
	$("#delete_product").dialog("close");
	if( M_oldProductEvent ) {
		$(M_oldProductEvent).remove();
		M_oldProductEvent = null;
	}
	$("#form").animate({width: 0},{
		duration: M_duration,
		complete: function() { deleteStop() }
	});
}
function deleteProduct(){
	
}
function setProductInfo() {
	var data = this.result;
	$("#save_product").attr("disabled", true);
	$("#status_on").removeClass("active");
	$("#status_off").removeClass("active");
	$("#name_am").val(data.name_am);
	$("#name_ru").val(data.name_ru);
	$("#name_en").val(data.name_en);
	$("#description_am").val(data.description_am);
	$("#description_ru").val(data.description_ru);
	$("#description_en").val(data.description_en);
	$("#price").val(data.price);
	if( data.status == 1 ){
		$("#status_on").addClass("active");
	} else {
		$("#status_off").addClass("active");
	}
}
function modification() {
	$("#save_product").removeAttr("disabled");
}
function getProductInfo() {
	var obj = {};
	obj.id = M_currentProductID;
	obj.name_am = $("#name_am").val();
	obj.name_ru = $("#name_ru").val();
	obj.name_en = $("#name_en").val();
	obj.description_am = $("#description_am").val();
	obj.description_ru = $("#description_ru").val();
	obj.description_en = $("#description_en").val();
	obj.description_en = $("#description_en").val();
	obj.price = $("#price").val();
	if( $("#status_on").hasClass("active") ) {
		obj.status = 1;
	} else {
		obj.status = 0;
	}
	return obj;
}
function save(){
	$("#save_product").attr("disabled", true);
	var obj = getProductInfo();
	$.remoteCall('MENU_product','updateProductInfo',[obj],callBackSave);
}
function callBackSave() {
	alert("OK");
}